package com.owc.service.impl.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.owc.dao.RechargeFlowMapper;
import com.owc.dao.TransferRecordMapper;
import com.owc.domain.BaseResult;
import com.owc.domain.dto.admin.ChainBean;
import com.owc.domain.dto.admin.RechargeDetail;
import com.owc.domain.dto.admin.RechargeListDTO;
import com.owc.domain.entity.RechargeFlow;
import com.owc.domain.entity.TransferRecord;
import com.owc.domain.vo.admin.RechargeQueryVo;
import com.owc.service.RechargeService;
import com.owc.service.admin.FinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Jackies
 * @Date 2019/12/16 21:33
 * @Description TODO://
 **/
@Service
public class RechargeServiceImpl implements RechargeService {

    @Resource
    private RechargeFlowMapper rechargeFlowMapper;
    @Resource
    private TransferRecordMapper transferRecordMapper;

    @Override
    public BaseResult<List<RechargeListDTO>> list(RechargeQueryVo rechargeQueryVo) {

        List<RechargeListDTO> rechargeListDTOS = new ArrayList<>();

        PageHelper.startPage(rechargeQueryVo.getPageIndex(),rechargeQueryVo.getPageSize());
        List<RechargeFlow> rechargeFlows = rechargeFlowMapper.selectByQueryModel(rechargeQueryVo);
        PageInfo<RechargeFlow> pageInfo = new PageInfo<>(rechargeFlows);

        rechargeFlows.forEach(rechargeFlow -> {
            rechargeListDTOS.add(new RechargeListDTO().convertFrom(rechargeFlow));
        });

        return BaseResult.success(rechargeListDTOS,pageInfo.getTotal());
    }

    @Override
    public RechargeDetail detail(Long id) {

        RechargeDetail detail = new RechargeDetail();

        //获取订单基本信息
        RechargeFlow rechargeFlow = rechargeFlowMapper.selectByPrimaryKey(id);
        detail.setDetail(new RechargeDetail.DetailBean().convertFrom(rechargeFlow));

        //获取区块链交易详情
        List<TransferRecord> recordList = transferRecordMapper.selectByFlowId(rechargeFlow.getTransferId());
        if(recordList != null && !recordList.isEmpty()){
            detail.setChain(new ChainBean().convertFrom(recordList.get(0)));
        }else{
            detail.setChain(new ChainBean());
        }

        detail.setFlowList(new ArrayList<>());

        return detail;
    }
}
